/**
 * Vosao CMS. Simple CMS for Google App Engine.
 * Copyright (C) 2009 Vosao development team
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * email: vosao.dev@gmail.com
 */

package org.vosao.plugins.csdata.service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.vosao.business.Business;

//import org.vosao.common.BCrypt;
//import org.vosao.entity.UserEntity;
//import org.vosao.enums.UserRole;

import org.vosao.i18n.Messages;
import org.vosao.plugins.csdata.dao.CsdataDao;
import org.vosao.plugins.csdata.entity.CsdataConfigEntity;
import org.vosao.plugins.csdata.entity.CsdataUserEntity;
import org.vosao.service.ServiceResponse;
import org.vosao.utils.StreamUtil;

public class CsdataBackServiceImpl extends AbstractCsdataService 
		implements CsdataBackService {

	private static final String REGISTER_FORM_TEMPLATE = 
		"org/vosao/plugins/csdata/resources/csdataForm.html";
	

	public CsdataBackServiceImpl(Business business, CsdataDao aCsdataDao) {
		super(business, aCsdataDao);
	}

	@Override
	public List<CsdataUserEntity> getCsdataUsers() {
		try {
			return getCsdataDao().getCsdataUserDao().select();
		}
		catch(Exception e) {
			e.printStackTrace();
			return Collections.EMPTY_LIST;
		}
	}

	@Override
	public CsdataConfigEntity getConfig() {
		CsdataConfigEntity config = getCsdataDao().getCsdataConfigDao()
				.getConfig();
		if (config.isNew()) {
			
			config.setCsdataFormTemplate(getTextResource(REGISTER_FORM_TEMPLATE));
			
		}
		return config;
	}

	private String getTextResource(String path) {
		try {
			return StreamUtil.getTextResource(this.getClass().getClassLoader(), 
					path);
		}
		catch (Exception e) {
			e.printStackTrace();
			return e.getMessage();
		}
	}
	
	@Override
	public ServiceResponse saveConfig(Map<String, String> vo) {
		CsdataConfigEntity config = getCsdataDao().getCsdataConfigDao()
				.getConfig();
		config.setCsdataFormTemplate(vo.get("csdataFormTemplate"));
		
		getCsdataDao().getCsdataConfigDao().save(config);
		return ServiceResponse.createSuccessResponse(
				Messages.get("success"));
	}


	@Override
	public ServiceResponse restoreCsdataFormTemplate() {
		CsdataConfigEntity config = getConfig();
		config.setCsdataFormTemplate(getTextResource(
				REGISTER_FORM_TEMPLATE));
		getCsdataDao().getCsdataConfigDao().save(config);
		return ServiceResponse.createSuccessResponse(
				config.getCsdataFormTemplate());
	}

	@Override
	public ServiceResponse removeCsdataUser(Long id) {
		CsdataUserEntity reg = getCsdataDao().getCsdataUserDao().getById(id);
		if (reg != null) {
			getCsdataDao().getCsdataUserDao().remove(id);
		}
		return ServiceResponse.createSuccessResponse(
				Messages.get("success"));
	}


}
